﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Kooboo.CMS.Content.Models;
using Kooboo.CMS.Content.Query;
using Kooboo.CMS.Sites.View;
using Kooboo.CMS.FoodOrdering.Areas.Customer.Models;
using Kooboo.CMS.Sites.Globalization;
namespace Kooboo.CMS.FoodOrdering.Areas.Customer.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            return View(LoginModel.Current ?? new LoginModel());
        }
        [HttpPost]
        public ActionResult Index(LoginModel model)
        {
            var user = ContentHelper.TextFolder(CustomerAreaRegistration.CustomerFolderName).CreateQuery()
                .WhereEquals("Name", model.Username)
                .WhereEquals("Password", model.Password).FirstOrDefault();
            if (user == null)
            {
                ModelState.AddModelError("", "The username or password is incorrect.".RawLabel().ToString());
            }
            else
            {
                model = new LoginModel() { Username = model.Username, IsAuthenticated = true, IsAdmin = (bool)user["Admin"] };
                LoginModel.Current = model;
            }
            return View(model);
        }
        public ActionResult Logout()
        {
            LoginModel.Current = null;
            return RedirectToAction("Index");
        }
    }
}
